package jp.ddo.trismegistos.service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import jp.ddo.trismegistos.framework.service.Service;
import jp.ddo.trismegistos.framework.util.DateUtil;
import jp.ddo.trismegistos.vo.Event;

public class EventService extends Service {

	public List<Event> getEventList() throws SQLException {
		final PreparedStatement ps = createSql("select * from EVENT where DELETE_FLG = '0'");
		return getList(ps, Event.class);
	}

	public void addEvent(final Event event) throws SQLException {

		event.setInsertDate(DateUtil.getNow());
		event.setId(getEventSeq());

		final PreparedStatement ps = createSql("insert into EVENT(ID, NAME, RESUME, DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, INSERT_DATE, DELETE_FLG) values (?,?,?,?,?,?,?,?,?,?)");
		ps.setInt(1, event.getId());
		ps.setString(2, event.getName());
		ps.setString(3, event.getResume());
		ps.setString(4, event.getDay1());
		ps.setString(5, event.getDay2());
		ps.setString(6, event.getDay3());
		ps.setString(7, event.getDay4());
		ps.setString(8, event.getDay5());
		ps.setDate(9, DateUtil.convertSqlDate(event.getInsertDate()));
		ps.setString(10, "0");

		exec(ps);
	}

	public int getEventSeq() throws SQLException {
		final PreparedStatement ps = createSql("update EVENT_SEQ SET ID = LAST_INSERT_ID(ID+1);");
		exec(ps);
		final PreparedStatement ps2 = createSql("select LAST_INSERT_ID();");
		return getInt(ps2);
	}
}
